#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import List, Dict

class ChangeTypeDetector:
    """修改类型判断器，负责判断diff的修改类型"""
    
    @staticmethod
    def determine_change_type(add_content: str, remove_content: str) -> str:
        """
        确定修改类型
        
        Args:
            add_content: 添加的内容
            remove_content: 删除的内容
            
        Returns:
            str: 修改类型 ('add', 'delete', 'modify', 'unknown')
        """
        if add_content and remove_content:
            return 'modify'
        elif add_content and not remove_content:
            return 'add'
        elif remove_content and not add_content:
            return 'delete'
        else:
            return 'unknown'
    
    @staticmethod
    def extract_change_pairs(hunk_changes: List[Dict]) -> List[tuple]:
        """
        从hunk changes中提取修改对
        
        Args:
            hunk_changes: hunk的修改列表
            
        Returns:
            List[tuple]: [(add_content, remove_content, context_before, context_after), ...]
        """
        change_pairs = []
        i = 0
        
        while i < len(hunk_changes):
            change = hunk_changes[i]
            
            if change['type'] in ['add', 'remove']:
                # 收集连续的修改
                add_content = ""
                remove_content = ""
                context_before = ""
                context_after = ""
                
                # 获取前面的上下文
                if i > 0 and hunk_changes[i-1]['type'] == 'context':
                    context_before = hunk_changes[i-1]['content']
                
                # 收集连续的add和remove
                while i < len(hunk_changes) and hunk_changes[i]['type'] in ['add', 'remove']:
                    if hunk_changes[i]['type'] == 'add':
                        add_content += hunk_changes[i]['content']
                    elif hunk_changes[i]['type'] == 'remove':
                        remove_content += hunk_changes[i]['content']
                    i += 1
                
                # 获取后面的上下文
                if i < len(hunk_changes) and hunk_changes[i]['type'] == 'context':
                    context_after = hunk_changes[i]['content']
                
                change_pairs.append((add_content, remove_content, context_before, context_after))
            else:
                i += 1
        
        return change_pairs
